﻿using System;
using System.Collections.Generic;
using ArtMath.Core.Data;
using ArtMath.Core.Helper;

namespace ArtMath.PointCollection
{
    [Serializable]
    public class Circle : CurveDescribable
    {
        public PointD Center;
        public double Radius;
        public Circle()
        {

        }
        public Circle(PointD center, double r)
        {
            Center = center;
            Radius = r;
        }
        public override bool Contains(PointD pt)
        {
            return Radius.AlmostEqual(Center.DistanceTo(pt)) && AfterContains(pt);
        }
        public override PointD? HeadPoint
        {
            get
            {
                return null;
            }
        }
        public override PointD? TailPoint
        {
            get
            {
                return null;
            }
        }
        public override double GetSlideExcess(PointD currentLocation, double length)
        {
            return double.NegativeInfinity;
        }
        public override PointD? GetNearestPoint(PointD pt)
        {
            return Center.Offset(Center.GetAngle(pt), Radius);
        }
        public override PointD SlideDestination(PointD currentLocation, double length, out bool reverse)
        {
            reverse = false;
            return Center.Offset(Center.GetAngle(currentLocation) + length / Radius, Radius);
        }
        protected override PointCollectionBase Intersect(PointCollectionBase collection)
        {
            HashSet<PointD> set = new HashSet<PointD>();
            Circle cir = collection as Circle;
            if (cir != null)
            {
                set.AddRange(CurveIntersection.CirclesIntersection(Center, Radius, cir.Center, cir.Radius));
            }
            if (set.Count == 0) return Empty.Instance;
            return AfterIntersect(collection.AfterIntersect(new DiscontinuityPoint(set)));
        }
    }
}
